//
//  shareFunctionCJ.m
//  BuyHomeTeamPro
//
//  Created by wy on 17/5/11.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "shareFunctionCJ.h"
#import "YXCustomActionSheet.h"
#import "WXApi.h"
#import "WXApiObject.h"

#import "CJFileCacheManager.h"
@interface shareFunctionCJ ()<YXCustomActionSheetDelegate>

{
    NSString *_urlString;
    NSString *_titleString;
    NSString *_imageUrlString;
    NSString *_detailTitle;
    
    UIImage *_shareImage;
    
    NSInteger _type; // 1是快讯 其他不适
    
}
@property (nonatomic, strong)UIView *View;
@property (nonatomic, strong)CJBaseViewController *VC;

@end

@implementation shareFunctionCJ
+(instancetype)shareFunction{
    static dispatch_once_t onceToken;
    static shareFunctionCJ *instance;
    dispatch_once(&onceToken, ^{
        instance = [[shareFunctionCJ alloc] init];
        
    });
    return instance;
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)weinxinShareResult:(NSNotification *)result{
    NSString *WXresult = [CJFileCacheManager readUserDataForKey:@"weixinShareResult"];
    if ([WXresult isEqualToString:@"微信分享成功"]) {
        if (self.ShareWXSuccessBlock) {
            self.ShareWXSuccessBlock();
        }
//        [CJUIKit showSuccessWithStatus:WXresult];
    }else{
        [CJUIKit showErrorWithStatus:WXresult];
      
        
    }
    
}
-(void)shareInfoP:(UIView *)view viewController:(CJBaseViewController *)VC WithUrlSTr:(NSString *)url WithTitle:(NSString *)titleStr withImageViewUrlStr:(NSString *)imgurlStr withDetailTitleString:(NSString *)detailSt withType:(NSInteger)type{
    
    if ([CJUIKit isNullWithString:url]) {
        _urlString = @"www.baidu.com";
    }else{
        _urlString = url;
    }
    if ([CJUIKit isNullWithString:titleStr]) {
        _titleString = @"VE财经";
    }else{
        _titleString = titleStr;
    }
    if ([CJUIKit isNullWithString:imgurlStr]) {
        _imageUrlString = nil;
    }else{
        _imageUrlString = imgurlStr;
    }
    if ([CJUIKit isNullWithString:detailSt]) {
        _detailTitle = @"我正在使用的这个觉得真不错,推荐给你哦～";
    }else{
        _detailTitle = detailSt;
    }
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(weinxinShareResult:) name:@"weixinShare" object:nil];
    self.View = view;
    self.VC = VC;
    YXCustomActionSheet *custShare = [[YXCustomActionSheet alloc] init];
    custShare.delegate = self;

    NSArray *contentArr = @[@{@"name":@"微信好友",@"icon":@"微信"},@{@"name":@"朋友圈",@"icon":@"朋友圈"}];
    [custShare showInView:[UIApplication sharedApplication].keyWindow contentArray:contentArr withType:type withTitle:titleStr andContent:detailSt];
    
    _shareImage = [BHJProjectHelper imageFromView:custShare.messageView];

    _type = type;

    
}
-(void)customActionSheetButtonClick:(YXActionSheetButton *)btn{


    
    if (btn.tag == 0) {
        //微信好友
        [self weixinActionShareUrlStr:_urlString];
    }else if (btn.tag == 1){
        //朋友圈
        [self weixinFriendsActionShareUrlStr:_urlString];
    }
}


-(void)weixinFriendsActionShareUrlStr:(NSString *)urlStr{
    
//    BOOL isInstallation = [WXApi isWXAppInstalled];
//    if (isInstallation == NO) {
//        [CJUIKit showErrorWithStatus:@"请安装微信后使用分享功能"];
//    }else{
    
    
    if (_type == 1) {
        NSData *imageData = UIImageJPEGRepresentation(_shareImage, 1);
        UIImage *thumbImage = [UIImage imageWithData:UIImageJPEGRepresentation(_shareImage, 0.5)];
        
        WXImageObject *ext = [WXImageObject object]; // 小于10MB
        ext.imageData = imageData;
        WXMediaMessage *message = [WXMediaMessage message];
        message.mediaObject = ext;
        // 缩略图 小于32KB
        [message setThumbImage:[UIImage imageNamed:@"VE_32"]];
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.scene = WXSceneTimeline;
        req.message = message;
        [WXApi sendReq:req];
    } else {
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        //        req.text = urlStr;
        req.bText = NO;
        req.scene = WXSceneTimeline;
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = _titleString;
        if (_detailTitle.length == 0) {
            message.description = @"";
        }else{
            message.description = _detailTitle;
        }
        /*
         
         //1.拼接网络解析到的图片的全路径(图片基础路径+你解析到的图片名字)=cover_PIC这个字符串
         NSString *cover_PIC  = [NSString stringWithFormat:@"%@%@",kPublicInterfaceAPI,_imageUrlString];
         //2.要把这个字符串转成image类型准备压缩
         UIImage *image_pic=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cover_PIC]]];
         NSLog(@"image_pic=======%@",image_pic);
         //3.图片压缩 （大小不能超过32K）
         UIImage *resultImage = [UIImage cj_compressImageSize:image_pic toByte:1024*32];
         
         [message setThumbImage:resultImage];
         */
        [message setThumbImage:[UIImage imageNamed:@"VE_32"]];
        
        
        WXWebpageObject *webPageobject = [WXWebpageObject object];
        webPageobject.webpageUrl = urlStr;
        message.mediaObject = webPageobject;
        req.message = message;
        [WXApi sendReq:req];
    }

//    }
    
}



-(void)weixinActionShareUrlStr:(NSString *)urlStr{
//    BOOL isInstallation = [WXApi isWXAppInstalled];
//    if (isInstallation == NO) {
//        [CJUIKit showErrorWithStatus:@"请安装微信后使用分享功能"];
//    }else{
    
    if (_type == 1) {
        
        NSData *imageData = UIImageJPEGRepresentation(_shareImage, 1);
        UIImage *thumbImage = [UIImage imageWithData:UIImageJPEGRepresentation(_shareImage, 0.5)];

        WXImageObject *ext = [WXImageObject object]; // 小于10MB
        ext.imageData = imageData;
        WXMediaMessage *message = [WXMediaMessage message];
        message.mediaObject = ext;
        // 缩略图 小于32KB
        [message setThumbImage:[UIImage imageNamed:@"VE_32"]];
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.scene = WXSceneSession;
        req.message = message;
        [WXApi sendReq:req];

    } else {
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        
        NSLog(@"%@",urlStr);
        req.bText = NO;
        req.scene = WXSceneSession;
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = _titleString;
        if (_detailTitle.length == 0) {
            message.description = @"";
        }else{
            message.description = _detailTitle;
        }
        
     
        if (![BHJProjectHelper isNilOrNull:_imageUrlString]) {
            //1.拼接网络解析到的图片的全路径(图片基础路径+你解析到的图片名字)=cover_PIC这个字符串
            NSString *cover_PIC  = [NSString stringWithFormat:@"%@",_imageUrlString];
            
            // 2.要把这个字符串转成image类型准备压缩
            UIImage *image_pic=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cover_PIC]]];
            
            // 3.图片压缩
            UIImage *resultImage = [UIImage cj_compressImageSize:image_pic toByte:(1024*32)];
            
            [message setThumbImage:resultImage];
        } else {
            [message setThumbImage:[UIImage imageNamed:@"VE_32"]];
            
        }

        WXWebpageObject *webPageobject = [WXWebpageObject object];
        webPageobject.webpageUrl = urlStr;
        message.mediaObject = webPageobject;
        req.message = message;
        [WXApi sendReq:req];
    }
    
    

//    }
    
}






@end
